package com.ymatou.iapi.auth.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.SignAuthRequestDtoBean;
import com.ymatou.iapi.auth.service.SignAuthRequestDtoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.mongo.UserTokenWapper;
/**
*验证token
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SignAuthRequestDto{
	private static SignAuthRequestDtoBean signauthrequestdtoBean;
	private static SignAuthRequestDtoCall signauthrequestdtoCall;
	private UserTokenWapper userTokenWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("验证token");
	}

	@Before
	public void caseUp() throws Exception {
		signauthrequestdtoBean=new SignAuthRequestDtoBean();
		signauthrequestdtoCall=new SignAuthRequestDtoCall();
		userTokenWapper=new UserTokenWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(signauthrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_SignAuthRequestDto_001() {
		Logger.start(true,"有效的token,返回成功");
		try {
			UserRegisterInfoRequestDtoBean cur = new UserRegisterInfoRequestDtoBean();
			cur = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc =JUserCallService.UserRegisterInfoRequestDtoCall(cur); 
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			signauthrequestdtoBean.setToken(AccessToken);
			signauthrequestdtoBean.setSClientType("testtype");
			signauthrequestdtoBean.setDeviceId("aa");
			signauthrequestdtoBean.setTokenSource("App");
			signauthrequestdtoBean.setClientIp("0.0.0.0");
			
			signauthrequestdtoCall.setData(signauthrequestdtoBean);
			signauthrequestdtoCall.callService();	
			
			Logger.verifyEquals(true, signauthrequestdtoCall.getSuccess(), "验证Success");
			Logger.verifyEquals(200, signauthrequestdtoCall.getCode(), "验证Code");
			

			Map<String, Object> userlst= userTokenWapper.getUserUidByToken(AccessToken);
			if(userlst!=null && userlst.size()> 0)
			{
			  Logger.verifyEquals(userlst.get("uId"), signauthrequestdtoCall.getUserId(), "验证uid");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Ts_SignAuthRequestDto_002() {
		Logger.start(true,"无效的token,返回失败");
		try {
			signauthrequestdtoBean.setToken("123321");
			signauthrequestdtoBean.setSClientType("testtype");
			signauthrequestdtoBean.setDeviceId("aa");
			signauthrequestdtoBean.setTokenSource("App");
			signauthrequestdtoBean.setClientIp("0.0.0.0");
			
			signauthrequestdtoCall.setData(signauthrequestdtoBean);
			signauthrequestdtoCall.callService();	
			
			Logger.verifyEquals(false, signauthrequestdtoCall.getSuccess(), "验证Success");
			Logger.verifyEquals(-160, signauthrequestdtoCall.getCode(), "验证Code");
			
			Logger.verifyEquals("未找到token", signauthrequestdtoCall.getLastErrorMessage(), "验证LastErrorMessage");
//			Map<String, Object> userlst= userTokenWapper.getUserUidByToken("123321");
//			if(userlst!=null && userlst.size()> 0)
//			{
//			  Logger.verifyEquals("", signauthrequestdtoCall.getUserId(), "验证token");
//			}			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}